﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace FuckYoukuAds
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window, INotifyPropertyChanged
    {
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = this;
            RefreshTempath();
        }

        public string Tempath { get; set; }
        public bool Operatable { get { return !string.IsNullOrWhiteSpace(Tempath); } }

        public event PropertyChangedEventHandler PropertyChanged;

        void RaisePropertyChangedEvent(string PropertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(PropertyName));
        }

        void RefreshTempath()
        {
            DirectoryInfo flashshared = new DirectoryInfo(string.Format("{0}/macromedia/flash player/#sharedobjects/", Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)));
            Tempath = "";
            if (flashshared.Exists)
            {
                var subs = flashshared.GetDirectories();
                if (subs.Length > 0)
                {
                    TempathInfo = subs[0];
                    Tempath = subs[0].Name;
                }
                else
                {
                    MessageBox.Show("尚未生成临时文件夹");
                }
            }
            else
            {
                MessageBox.Show("未找到flash本地存储");
            }
            RaisePropertyChangedEvent("Tempath");
            RaisePropertyChangedEvent("Operatable");
        }

        private void BtnRefreshTemp_Click(object sender, RoutedEventArgs e)
        {
            RefreshTempath();
        }

        DirectoryInfo TempathInfo;
        public ObservableCollection<string> fuckees { get; private set; } = new ObservableCollection<string>() { "irs01.net", "js.tudouui.com", "static.youku.com" };

        private void BtnFuckIt_Click(object sender, RoutedEventArgs e)
        {
            // 移除文件夹并且用生成空文件代替
            for (int i = 0; i < fuckees.Count; i++)
            {
                var name = string.Format("{0}/{1}", TempathInfo.FullName, fuckees[i]);
                var dd = new DirectoryInfo(name);
                if (dd.Exists)
                {
                    dd.Delete(true);
                    File.Create(name).Close();
                }
            }
            MessageBox.Show("Yes, Ads' fucked.");
        }

        public string NewFuckee { get; set; } = "";
        private void BtnNewFuckee_Click(object sender, RoutedEventArgs e)
        {
            fuckees.Add(NewFuckee);
        }

        private void BtnRemoveFuckee_Click(object sender, RoutedEventArgs e)
        {
            var str = (sender as Button).DataContext as string;
            fuckees.Remove(str);
        }
    }
}
